<template>
  <div class="member-order-page">
    <XtxTabs v-model="activeName" @tab-click="tabClick">
      <XtxTabsPanel
        v-for="item in orderStatus"
        :key="item.name"
        :label="item.label"
        :name="item.name"
      ></XtxTabsPanel>
    </XtxTabs>
    <!-- 订单列表 -->
    <div class="order-list">
      <div v-if="loading" class="loading"></div>
      <div class="none" v-if="!loading && ordersList.length === 0">
        暂无数据
      </div>
      <OrderItem
        v-for="order in ordersList"
        :key="order.id"
        :order="order"
        @on-cancel="handelCancel"
        @on-delete="handelDel"
        @on-confirm="handelConfirm"
        @on-logistics="handelLogistics"
      />
    </div>
    <!-- 分页组件 -->
    <XtxPagination
      v-if="total > 0"
      :total="total"
      :currentPage="reqParams.page"
      :pageSize="reqParams.pageSize"
      @change="reqParams.page = $event"
    />
    <!-- 取消原因组件 -->
    <OrderCancel ref="orderCancelRef" />
    <!-- 查看物流组件 -->
    <OrderLogistics ref="orderLogisticsRef" />
  </div>
</template>
<script>
import { reactive, ref, watch } from 'vue'
import OrderItem from './components/order-item.vue'
import OrderCancel from './components/order-cancel.vue'
import OrderLogistics from './components/order-logistics.vue'
import { getOrderList, deleteOrder, confirmOrder } from '@/api/order'
import { orderStatus } from '@/api/constants'
import Confirm from '@/components/library/Confirm'
import Message from '@/components/library/Message'
export default {
  name: 'MemberOrder',
  components: { OrderItem, OrderCancel, OrderLogistics },
  setup () {
    const activeName = ref('all')
    const tabClick = ({ index }) => {
      reqParams.orderState = index
      reqParams.page = 1
    }
    // 获取订单列表
    const ordersList = ref([])
    const reqParams = reactive({
      page: 1,
      pageSize: 10,
      orderState: 0
    })
    const loading = ref(false)
    const total = ref(0)
    const getOrder = () => {
      loading.value = true
      getOrderList(reqParams).then((data) => {
        ordersList.value = data.result.items
        total.value = data.result.counts
        loading.value = false
      })
    }
    watch(
      reqParams,
      () => {
        getOrder()
      },
      { immediate: true }
    )

    // 删除订单
    const handelDel = (orderId) => {
      Confirm({ text: '您确定删除该订单吗？' }).then(() => {
        deleteOrder([orderId]).then(() => {
          Message({ type: 'success', text: '删除订单成功' })
          getOrder()
        })
      })
    }

    return {
      activeName,
      tabClick,
      ordersList,
      loading,
      total,
      reqParams,
      orderStatus,
      handelDel,
      ...useConfirm(),
      ...useLogistics(),
      ...useCancel()
    }
  }
}
// 提供复用的函数
export const useCancel = () => {
  // 取消订单
  const orderCancelRef = ref(null)
  const handelCancel = (order) => {
    orderCancelRef.value.open(order)
  }
  return {
    orderCancelRef,
    handelCancel
  }
}

export const useLogistics = () => {
  // 查看物流
  const orderLogisticsRef = ref(null)
  const handelLogistics = (order) => {
    orderLogisticsRef.value.open(order)
  }
  return {
    orderLogisticsRef,
    handelLogistics
  }
}

export const useConfirm = () => {
  // 确认收货
  const handelConfirm = (order) => {
    Confirm({ text: '您确定收货吗？' }).then(() => {
      confirmOrder(order.id).then(() => {
        Message({ type: 'success', text: '确认收货成功' })
        order.value.orderState = 4
      })
    })
  }
  return { handelConfirm }
}
</script>
<style scoped lang="less">
.order-list {
  background: #fff;
  padding: 20px;
  position: relative;
}
.loading {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  background: rgba(255, 255, 255, 0.9) url(../../../assets/images/loading.gif)
    no-repeat center;
}
.none {
  height: 400px;
  text-align: center;
  line-height: 400px;
  color: #999;
}
</style>
